//获取应用实例
const app = getApp()
let taskId;

Page({
  data: {
    imgList: [],
    index:'',
    ybArray: [],
  },
  onLoad: function (options) {
    var self = this;
    taskId = options.taskId;
    //加载佣兵等级列表
    wx.request({
      url:app.globalData.prefix+'/taskAction/levelListPerson.do',
      success:function(res){
        let levelList = res.data.result;
        self.setData({ybArray:levelList});
        //加载任务信息
        wx.request({
          url:app.globalData.prefix+'/taskAction/toMergePublish?taskId='+taskId,
          success:function(res){
            if(res.data.status!='00'){
              wx.$showToast(res.data.errorMsg);
              return;
            }
            self.setData({task:res.data.result});
            self.setData({imgList:res.data.result.taskImage.split(';')});
            for(let i=0;i<levelList.length;i++){
              if(levelList[i].levelId==res.data.result.taskLevelId){
                self.setData({index:i});
              }
            }
          }
        });
      },
      fail:function(res){
        wx.$showToast("系统错误");
      }
    });
    
  },
  //图片上传
  upload() {
    let _this = this
    wx.chooseImage({
      success(res) {
        if (_this.data.imgList.length + res.tempFilePaths.length > 5) {
          wx.$showToast('任务插图不得超过五张！')
          return
        }

        wx.uploadFile({
          filePath: res.tempFilePaths[0],
          name: 'file',
          url: app.globalData.prefix+'/imageAction/uploadImage.do',
          success:function(res1){
            let result = JSON.parse(res1.data);
            _this.data.imgList = _this.data.imgList.concat(result.result)
            _this.setData({
              imgList:_this.data.imgList
            })
          }
        })
      }
    })
  },
  //删除图片
  delet(e) {
    this.data.imgList.splice(e.currentTarget.dataset.index, 1) 
    this.setData({
      imgList:this.data.imgList
    })
  },
  bindPickerChange: function (e) {
    this.setData({
       index: e.detail.value
    })
  },
  formSubmit(e) {
    let self = this;
    let taskName = e.detail.value.title;
    let taskInfo = e.detail.value.taskDetaily;
    let levelId = e.detail.value.levelId;
    let taskImage = '';
    let taskImageArray = self.data.imgList;
    for(let i=0;i<taskImageArray.length;i++){
      taskImage += taskImageArray[i];
      if(i<taskImageArray.length-1){
        taskImage += ';';
      }
    }
    if (taskName == '') {
      wx.$showToast('任务标题不可为空哦！')
      return;
    }
    if(levelId == ''){
      wx.$showToast('请选择人物限制等级')
      return;
    }
    console.log('开始发送新增请求...')
    wx.request({
      url:app.globalData.prefix+'/taskAction/taskUpdate.do',
      data:{
        taskId:taskId,
        taskName:taskName,
        taskInfo:taskInfo,
        levelId:levelId,
        taskImage:taskImage
      },
      success:function(res){
        if(res.data.status!='00'){
          wx.$showToast(res.data.errorMsg);
          return;
        }else{
          let status = res.data.result;
          if(status=='05'){
            wx.navigateTo({
              url:'../payMoney/payMoney?taskId='+taskId
            })
          }else if(status=='00'){
            wx.navigateTo({
              url: '../mySelfTask/mySelfTask?index=1',
            })
          }
        }
      },
      fail:function(e){
        wx.$showToast("系统错误");
        console.log(e);
      }
    });
  }
})
